package com.bs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.entity.AdminInfo;
import com.bs.entity.Employee;
import com.bs.entity.EmployeeInfo;
import com.bs.service.ClockInfoService;
import com.bs.service.EmployeeInfoService;
import com.bs.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/EmployeeInfo")
public class EmployeeInfoCon {
    @Autowired
    EmployeeInfoService employeeInfoService;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    ClockInfoService clockInfoService;

    @GetMapping("/selectById")
    public EmployeeInfo selectById(@RequestParam Integer id){

        return employeeInfoService.getById(id);
    }

    @GetMapping("/selectALL")
    public List<EmployeeInfo> selectAll(){
        return null;
    }

    @GetMapping("/login")
    public Boolean login(@RequestParam Integer id,
                         @RequestParam String password){
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>();
        queryWrapper.lambda().eq(Employee::getId,id).eq(Employee::getPassword,password);
        Employee one = employeeService.getOne(queryWrapper);
        if (Objects.isNull(one)){
            return false;
        }else {
            return true;
        }
    }

    //    新增和修改管理员
    @PostMapping("/saveEmployeeInfo")
    public Boolean saveEmployeeInfo(EmployeeInfo employeeInfo){
        return employeeInfoService.saveEmployeeInfo(employeeInfo);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean deleteEmployeeInfo(@PathVariable Integer id){
        return employeeInfoService.removeById(id);
    }


    @GetMapping("/page")
    public IPage<EmployeeInfo> findPage(@RequestParam Integer pageNum
            , @RequestParam Integer pageSize
            , @RequestParam(required = false) String name
            , @RequestParam(required = false) Integer id){
        IPage<EmployeeInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<EmployeeInfo> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(name)){
            queryWrapper.lambda().like(EmployeeInfo::getName,name);
        }
        if(Objects.nonNull(id)){
            queryWrapper.lambda().eq(EmployeeInfo::getId,id);
        }
        return employeeInfoService.page(page, queryWrapper);
    }

    @PostMapping("/changePwd")
    public Boolean changePwd(@RequestBody Map<String,String> data){

        Integer passwordFlag = clockInfoService.getPwd();
        if(passwordFlag == 0){
            return false;
        }
        Integer id = Integer.valueOf(data.get("id"));
        String password = data.get("password");
        String rePassword = data.get("repassword");
        return employeeService.updatePwd(id,password,rePassword);
    }
}
